package sysAuth

import sysUtil.ResultCode
import sysUtil.ResultJson
import sysUtil.SysCommUtil
import xdInterface.XdControllerInterface

import java.sql.SQLException

class SysViewCfgController implements XdControllerInterface {

    def index() {query()}

    @Override
    def delete() {
        del()
    }

    @Override
    def detail() {

    }

    @Override
    def del() {

    }

    @Override
    def edit() {
        def result = ResultJson.resultJson()

        try {
            def syscol = SysViewCfg.first()
            def field = params.field
            def value = params?.value
            if(syscol){
                syscol.setProperty(field,value)
                syscol.save(flush: true)
            }

            result.put(ResultJson.CODE.name, ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.MSG.name, ResultCode.OPT_SUCCESS.message)
        } catch (Exception e) {
            result.put(ResultJson.CODE.name, ResultCode.OPT_ERROR.code)
            result.put(ResultJson.MSG.name, ResultCode.OPT_ERROR.message)
            e.printStackTrace()
        } finally {
            //println result;
            render result.toString()
        }
    }

    @Override
    def save() {
        return null
    }

    @Override
    def update() {
        def result = ResultJson.resultJson()

        try {
            def id = Integer.parseInt(params.id)
            def  syscol = SysViewCfg.get(id)
            if(syscol){
                syscol.setProperty("webName",params.get("webName"))
                syscol.setProperty("sysName",params.get("sysName"))
                syscol.setProperty("uploadType",params.get("uploadType"))
                syscol.setProperty("maxUpload",params.get("maxUpload"))
                syscol.setProperty("copyRight",params.get("copyRight"))
                syscol.setProperty("emailUser",params.get("emailUser"))
                syscol.setProperty("emailPassWord",params.get("emailPassWord"))
                syscol.setProperty("emailHose",params.get("emailHose"))
                syscol.setProperty("indexLink",params.get("indexLink"))
                syscol.setProperty("indexLinkName",params.get("indexLinkName"))
                syscol.setProperty("tokenTime",Integer.parseInt(String.valueOf(params.get("tokenTime"))))

                syscol.setUpdater(params.updater)

                syscol.save(flush: true)
            }

            result.put(ResultJson.CODE.name, ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.MSG.name, ResultCode.OPT_SUCCESS.message)
            result.put(ResultJson.DATA.name, SysCommUtil.domainToMap(syscol))
        } catch (Exception e) {
            result.put(ResultJson.CODE.name, ResultCode.OPT_ERROR.code)
            result.put(ResultJson.MSG.name, ResultCode.OPT_ERROR.message)
            e.printStackTrace()
        } finally {
            //println result;
            render result.toString()
        }
    }

    @Override
    def add() {
        return null
    }

    @Override
    def dowLoad() {
        return null
    }

    @Override
    def upLoad() {

        def realPath = grailsApplication.getMainContext().servletContext.getRealPath("")
        def result = ResultJson.resultJson()
        def file = request.getFile("file") //params?.file
        def filedir
        try {

            def value = params.value
            def field = params.field
            if(file){
                def path = realPath + "\\images"
                filedir = new File(path)
                if( filedir.isDirectory() && !filedir.exists()){
                    filedir.mkdir()
                }
                filedir = new File(path,value)
                if(filedir){
                    if( !filedir.delete() ){
                        System.gc()
                        filedir.delete()
                    }
                }
                file.transferTo(new File(path,value))
            }

            def id = Integer.parseInt(params.id)
            def  syscol = SysViewCfg.get(id)
            if(syscol){
                syscol.setProperty(field,value)
                syscol.setUpdater(params.updater)
                syscol.save(flush: true)
            }
            result.put(ResultJson.DATA.name,SysCommUtil.domainToMap(syscol))
            result.put(ResultJson.CODE.name, ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.MSG.name, ResultCode.OPT_SUCCESS.message)
        } catch (IOException e) {
            result.put(ResultJson.CODE.name, ResultCode.IO_READ_ERROR.code)
            result.put(ResultJson.MSG.name, ResultCode.IO_READ_ERROR.message)
            e.printStackTrace()
        } catch (SQLException e) {
            result.put(ResultJson.CODE.name, ResultCode.OPT_ERROR.code)
            result.put(ResultJson.MSG.name, ResultCode.OPT_ERROR.message)
            e.printStackTrace()
        } finally {
            //println result;
            if(file){
                file = null
            }
            if(filedir){
                filedir = null
            }
            render result.toString()
        }
    }

    @Override
    def query() {
        return null
    }

    @Override
    def getOne() {
        def result = ResultJson.resultJson()
        try {
            SysViewCfg sysViewCfg = SysViewCfg.first()
            result.put(ResultJson.CODE.name, ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.MSG.name, ResultCode.OPT_SUCCESS.message)
            result.put(ResultJson.DATA.name,SysCommUtil.domainToMap(sysViewCfg))
        } catch (Exception e) {
            result.put(ResultJson.CODE.name, ResultCode.OPT_ERROR.code)
            result.put(ResultJson.MSG.name, ResultCode.OPT_ERROR.message)
            e.printStackTrace()
        } finally {
            //println result;
            render result.toString()
        }
    }


}
